package com.mjk.common.tools.lock.distrib.conf;

import org.redisson.Redisson;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.RedisTemplate;

@Configuration
public class DistribConf {
	@Value(value="${bee.dubbo.registry.address:127.0.0.1:2181}")
	private String zkHost;
	
	@Value(value="${bee.redis.host:127.0.0.1}")
	private String redisHost;
	
	@Value(value="${bee.redis.port:6379}")
	private String redisPort;

    public DistribConf(RedisTemplate redisTemplate) {
    }

    @Bean
	@ConditionalOnProperty(value = "bee.redis", havingValue = "true", matchIfMissing = false)
	public Redisson getRedisson() {
		Config config = new Config();
		config.useSingleServer().setAddress("redis://"+redisHost+":"+redisPort);
		return (Redisson) Redisson.create(config);
	}


    public String getZkHost(String host){
		if(host.contains("zookeeper://")){
			return host.replace("zookeeper://", "");
		}
		return host;
	}
}
